package net.aaa.hotel.controller;

import com.alibaba.fastjson.JSON;
import net.aaa.hotel.dao.RukuDao;
import net.aaa.hotel.dao.impl.RukuDaoImpl;
import net.aaa.hotel.entity.PageInfo;

import net.aaa.hotel.util.LayDate;
import net.aaa.hotel.util.LayuiTable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;



@RestController
@RequestMapping("/ruku")
public class RukuSpringController {
    @Autowired
    private RukuDao dao = new RukuDaoImpl();

    @RequestMapping("/query")
    public LayuiTable query(PageInfo pagl) {
        List<Map> map = dao.query(pagl);
        int count = dao.count(pagl);
        return new LayuiTable(count, map);
    }

    @RequestMapping("/queryId")
    public LayuiTable queryId(Integer id) {
     //   System.out.println(id);
        List<Map> map = dao.queryRuku(id);
        return new LayuiTable(map.size(), map);
    }

    @RequestMapping("/queryType")
    public List<Map> queryType() {
        List<Map> map = dao.queryType();
        return map;
    }

    @RequestMapping("/querySs")
    public List<Map> querySs(Integer id) {

        List<Map> map = dao.querySs(id);

        return map;
    }

    @RequestMapping("/querySc")
    public Map querySc(Integer id) {

        List<Map> map = dao.querySc(id);

        return map.get(0);
    }

    @RequestMapping("/add")
    public int add(HttpServletRequest request) {
        // 将接收到的表单数据转化成 map 用于添加入库单
        Map map = (Map) JSON.parse(request.getParameter("data"));
        // 将接收的入库表格数据 转化成 List<Map> 用于添加入库详情单 和 让图书数量增加

        List<Map> ta = (List<Map>) JSON.parse(request.getParameter("tableData"));

        int a = dao.addRu(map);
        int b = 0;

        for (Map map1 : ta) {
            b = dao.addRud(map1, a);
            /* c=dao.addS(map1);*/
        }
        return b;
    }

    @RequestMapping("/getRuKuMoney")
    public Map<String, List> getRuKuMoney(LayDate layDate) {

        Map<String, List> maps = dao.getRuKuMoney(layDate);
        return maps;
    }

    @RequestMapping("getAllTypeAndCount")
    public List<Map> getAllTypeAndCount() {
        List<Map> maps = dao.getAllTypeAndCount();
        return maps;
    }

    @RequestMapping("queryDingdDan")
    public int queryDingdDan(HttpSession session) {
        int i = dao.queryDingdDan();
        session.setAttribute("ding", i);
        return i;
    }

    @RequestMapping("addK")
    public int addK(Integer rkid) {

        List<Map> maps = dao.queryRuKuDatil(rkid);

        int c = 0;
        int a = 0;
        for (Map map : maps) {
            c += dao.addS(map);
        }

        if (c == maps.size()) {
            a = dao.upda(rkid);
        }

        return a;
    }
}
